package com.jingan.nursinghome.regulatory.select

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.Rect
import android.support.design.internal.BaselineLayout
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.Button
import android.widget.TextView
import cn.android.support.v7.lib.sin.crown.base.BaseActivity
import cn.android.support.v7.lib.sin.crown.base.BaseListenner
import cn.android.support.v7.lib.sin.crown.kotlin.anko.Titlebar
import cn.android.support.v7.lib.sin.crown.kotlin.base.BaseApplication
import cn.android.support.v7.lib.sin.crown.kotlin.base.BaseDialog
import cn.android.support.v7.lib.sin.crown.kotlin.bean.DateChoose
import cn.android.support.v7.lib.sin.crown.kotlin.common.Toast
import cn.android.support.v7.lib.sin.crown.kotlin.common.px
import cn.android.support.v7.lib.sin.crown.kotlin.dialog.DateChooseBetweenDialog
import cn.android.support.v7.lib.sin.crown.kotlin.dialog.RollerDialog
import cn.android.support.v7.lib.sin.crown.kotlin.popu.SpinnerPop
import cn.android.support.v7.lib.sin.crown.kotlin.utils.SelectorUtils
import cn.android.support.v7.lib.sin.crown.kotlin.widget.RoundTextView
import com.jingan.nursinghome.regulatory.data.MicrometerTable
import com.jingan.nursinghome.regulatory.dialog.LoginDialog
import com.jingan.nursinghome.regulatory.model.Qfinfo
import org.jetbrains.anko.*
import org.jetbrains.anko.recyclerview.v7.recyclerView
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.support.v4.swipeRefreshLayout
import com.jingan.nursinghome.regulatory.select.adapter.MyAdapter
import com.jingan.nursinghome.regulatory.select.adapter.MyAdapter2
import com.jingan.nursinghome.regulatory.view.H5
import kotlinx.coroutines.experimental.async
import java.util.ArrayList

/**
 * Created by 彭治铭 on 2018/7/15.
 */
class SelectMain(var activity: BaseActivity, isStatus: Boolean = true, isTransparent: Boolean = true) : BaseDialog(activity, isStatus = isStatus, isTransparent = isTransparent) {

    private var exitTime: Long = 0

    init {
        isDarkmode(true)
        isLocked(true) {
            if (System.currentTimeMillis() - exitTime > 2000) {
                Toast.show("再按一次退出")
                exitTime = System.currentTimeMillis()
            } else {
                cn.android.support.v7.lib.sin.crown.base.BaseApplication.getInstance().exit()
            }
            System.gc()
        }
        setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right)
    }


    var topBar: Titlebar.TopBar? = null
    override fun onCreateView(context: Context): View? {

        var type = MicrometerTable.getInstance().getUser().utype
        if (type.equals("1")) {
            utype = UTYPE_3//测试
        } else if (type.equals("2")) {
            utype = UTYPE_2//千分表
        } else if (type.equals("3")) {
            utype = UTYPE_3//领导端
        }
        //Log.e("test","type:\t"+type+"\tutype:\t"+utype)

        return context.UI {
            verticalLayout {
                backgroundColor = Color.parseColor("#F2F2F2")
                gravity = Gravity.CENTER_HORIZONTAL
                //第三个参数是返回键的颜色。此时是透明色。即返回键无效
                topBar = Titlebar.topBar(context as Activity, "领导端", Color.TRANSPARENT)
                topBar?.centerTextView?.apply {
                    textColor = Color.BLACK
                }
                topBar?.view.apply {
                    backgroundColor = Color.WHITE

                }
                addView(topBar?.view)
                topBar?.shadow = true
                topBar?.rightTextView?.apply {
                    text = "登出"
                    textColor = Color.BLACK
                    onClick {
                        loginOrOut(false)
                    }
                    //lparams只能在anko布局里面使用
                    //实例对象不能使用lparams，所以只能使用layoutParams
                    layoutParams.apply {
                        width = wrapContent
                        height = wrapContent
                    }
                }
                verticalLayout {
                    gravity = Gravity.CENTER_HORIZONTAL
                    backgroundColor = Color.parseColor("#F2F2F2")
                    var tabl = RoundTextView(context).apply {
                        gravity = Gravity.CENTER
                        text = "查看千分表"
                        textColor = Color.BLACK
                        textSize = px.textSizeX(36)
                        backgroundColor = Color.WHITE
                        SelectorUtils.selectorColor(this, Color.WHITE, Color.parseColor("#FFC7BC"))
                        onClick {
                            ismic = "1"//千分表
                            if (utype == UTYPE_2) {
                                SelectHome(activity, oid, true, true).show()//养老机构
                            } else if (utype == UTYPE_3) {
                                SelectOid(activity, true, true).show()//区级端
                            }

                        }
                    }.lparams {
                        width = px.x(650)
                        height = px.x(265)
                        topMargin = px.x(48)
                    }
                    addView(tabl)

                    var tab2 = RoundTextView(context).apply {
                        gravity = Gravity.CENTER
                        text = "查看日常表"
                        textColor = Color.BLACK
                        textSize = px.textSizeX(36)
                        backgroundColor = Color.WHITE
                        SelectorUtils.selectorColor(this, Color.WHITE, Color.parseColor("#FFC7BC"))
                        onClick {
                            ismic = "2"//千分表
                            if (utype == UTYPE_2) {
                                SelectHome(activity, oid, true, true).show()//养老机构
                            } else if (utype == UTYPE_3) {
                                SelectOid(activity, true, true).show()//区级端
                            }
                        }
                    }.lparams {
                        width = px.x(650)
                        height = px.x(265)
                        topMargin = px.x(48)
                    }
                    addView(tab2)
                }.lparams {
                    width = matchParent
                    height = matchParent
                }
            }
        }.view
    }

    //true登录，false退出登录
    private fun loginOrOut(login: Boolean) {
        LoginDialog.getInstance(activity, login, object : BaseListenner<Any?> {
            override fun onResult(result: Any?) {
                //登录成功回调

            }
        })
        dismiss()
    }

    companion object {
        var UTYPE_3 = 3//领导
        var UTYPE_2 = 2//千分表
        var utype = UTYPE_3
        var oid: String? = null
        var ismic = "1"// 字段。1 千分表   2 日常表
    }


    override fun listener() {
        if (utype == UTYPE_2) {
            topBar?.centerTextView?.setText("养老机构端")
            oid = MicrometerTable.getInstance().getUser().oid
        } else if (utype == UTYPE_3) {
            topBar?.centerTextView?.setText("区级端")
            oid = null
        }
    }


    override fun recycleView() {
    }


}